package net.awpspace.mp3player.utils;

import java.io.InputStream;
import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.VolumeControl;

import net.awpspace.mp3player.CONSTANT.Constants;
import net.awpspace.mp3player.entity.Mp3File;
import net.awpspace.mp3player.screen.PlayScreen;

public class AudioManager implements PlayerListener {

	private Vector playing;
	private Vector looping;

	/**
	 * Singleton
	 */
	private static AudioManager instance;

	private AudioManager() {
		playing = new Vector();
		looping = new Vector();
	}

	public static AudioManager getInstance() {
		if (instance == null) {
			instance = new AudioManager();
		}
		return instance;
	}

	/**
	 * Loop a file
	 * 
	 * @param filename
	 * @return
	 */
	public Player loopSample(Mp3File f) {
		Player p = playSample(f);
		looping.addElement(p);
		return p;
	}

	/**
	 * Play a file
	 * 
	 * @param filename
	 * @return
	 */
	public Player playSample(Mp3File f) {
		String filename = f.getPath();
		Player player = null;
		try {
			FileConnection connecetion = (FileConnection) Connector
					.open(filename);
			InputStream stream = connecetion.openDataInputStream();
			System.out.println("stream file");
			limitPlaybacks();
			player = Manager.createPlayer(stream, "audio/mp3");
			playing.addElement(player);
			player.realize();
			player.prefetch();
			player.addPlayerListener(this);
			player.start();
			System.out.println("Playing file");
		} catch (Exception e) {
		}
		return player;
	}

	/**
	 * Stop current player
	 * 
	 * @param player
	 */
	public void stopPlayer(Player player) {
		stop(player);
		looping.removeElement(player);
		playing.removeElement(player);
	}

	/**
	 * Stop all players
	 */
	public void stopAll() {
		for (int i = 0; i < playing.size(); i++) {
			stop((Player) playing.elementAt(i));
		}
		playing.removeAllElements();
		looping.removeAllElements();
	}

	public void playerUpdate(Player player, String event, Object eventData) {
		if (event.equals(PlayerListener.END_OF_MEDIA)) {
			PlayScreen.Notify();
			if (looping.contains(player)) {
				try {
					player.setMediaTime(0);
					player.start();
				} catch (Exception e) {
				}
			} else {
				stopPlayer(player);
			}
		}
	}
	
	public void setVolume(int value){
		if(playing !=null){
			for(int i=0; i<playing.size();i++){
				Player player = (Player) playing.elementAt(i);
				player.getControl("VolumeControl");
				VolumeControl vc =(VolumeControl) player.getControl("VolumeControl");
				vc.setLevel(value);
			}
		}
		if(looping != null){
			for(int i=0; i<looping.size();i++){
				Player player = (Player) looping.elementAt(i);
				player.getControl("VolumeControl");
				VolumeControl vc =(VolumeControl) player.getControl("VolumeControl");
				vc.setLevel(value);
			}
		}
	}

	/**
	 * Shuts down the player, that has been playing the longest
	 */
	private synchronized void limitPlaybacks() {
		try {
			while (playing.size() > Constants.MAX_PLAYERS) {
				Player player = (Player) playing.firstElement();
				playing.removeElementAt(0);
				stop(player);
			}
		} catch (Exception e) {
		}
	}

	/**
	 * Stop current player
	 * 
	 * @param player
	 */
	private synchronized void stop(Player player) {
		try {
			if (player.getState() == Player.STARTED) {
				try {
					player.stop();
				} catch (Exception e) {
				}
			}
			player.deallocate();
			player.close();
			player = null;
		} catch (Exception e) {
		}
	}
}
